﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Video;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class CameraGameOver : MonoBehaviour
{
    [Header("基础配置")]
    [Tooltip("选择标记位置")]
    public GameObject selectFlagObj;
    private int selectIndex = 0;
    [Tooltip("选择标记位置")]
    public Vector2[] selectFlagPositions = new Vector2[]
    {
        new Vector2(-46f,76f),
        new Vector2(-46f,52f),
    };

    [Header("调试配置")]
    [Tooltip("开启调试")]
    public bool debug = false;

    [Header("信息显示配置")]
    [Tooltip("P1成绩对象")]
    public GameObject P1ScoreObject;
    [Tooltip("P2成绩对象")]
    public GameObject P2ScoreObject;
    [Tooltip("P2成绩标签对象")]
    public GameObject P2ScoreLabelObject;
    [Tooltip("最高成绩对象")]
    public GameObject HIScoreObject;

    [Header("状态参数")]
    [Tooltip("Select键是否按下")]
    private bool selectPress = false;
    [Tooltip("Start键是否按下")]
    private bool startPress = false;


    private void Awake()
    {
        {
            if (P1ScoreObject.TryGetComponent(out Text text))
            {
                text.text = string.Format("{0:D}", ContraGlobal.Player1.Score);
            }
        }
        {
            if (HIScoreObject.TryGetComponent(out Text text))
            {
                text.text = string.Format("{0:D}", ContraGlobal.HIScroe);
            }
        }
        if (ContraGlobal.IsTwoPlayers())
        {
            P2ScoreObject.SetActive(true);
            P2ScoreLabelObject.SetActive(true);
            {
                if (P2ScoreObject.TryGetComponent(out Text text))
                {
                    text.text = string.Format("{0:D}", ContraGlobal.Player2.Score);
                }
            }
        }
        else
        {
            P2ScoreObject.SetActive(false);
            P2ScoreLabelObject.SetActive(false);
        }

    }
    private void Update()
    {
        selectPress = selectPress || Input.GetButtonDown("Select");
        startPress = startPress || Input.GetButtonDown("Start");
    }
    private void FixedUpdate()
    {
        UpdateSelect();
        UpdateStart();

        selectPress = startPress = false;
    }
    private void UpdateSelect()
    {

        if (selectPress)
        {
            selectIndex++;
            if (selectIndex > selectFlagPositions.Length - 1)
            {
                selectIndex = 0;
            }
        }
        if (selectFlagObj.TryGetComponent(out RectTransform rt))
        {
            rt.anchoredPosition = selectFlagPositions[selectIndex];
        }

    }

    private void UpdateStart()
    {
        if (startPress)
        {
            if (selectIndex == 0)
            {
                //ContraGlobal.currentLevel--;
                ContraGlobal.ResetLife();
                GoScene(ContraGlobal.SceneName_Map);
            }
            else if (selectIndex == 1)
            {
                ContraGlobal.currentLevel = 0;
                ContraGlobal.ResetLife();
                GoScene(ContraGlobal.SceneName_Title);
            }
        }
    }

    private void GoScene(string sceneName)
    {
        SceneManager.LoadScene(sceneName, LoadSceneMode.Single);
    }
}
